﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using TA.Turbo;
using System.Reflection;
using TA.Turbo.Controls;
using TA.DotNetUtils;

/// <summary>
/// Defines extension methods for the Control class.
/// </summary>
public static class _ControlExtensions
{
	/// <summary>
	/// Creates a <see cref="LiteralControl"/> and adds it to this control's children collection.
	/// </summary>
	/// <param name="cThis"></param>
	/// <param name="literal"></param>
	public static void AppendLiteral(this Control cThis, string literal)
	{
		cThis.Controls.Add(new LiteralControl(literal));
	}
	/// <summary>
	/// Creates a <see cref="LiteralControl"/> and inserts it at the specified index into this control's children collection.
	/// </summary>
	/// <param name="cThis"></param>
	/// <param name="index"></param>
	/// <param name="literal"></param>
	public static void InsertLiteral(this Control cThis, int index, string literal)
	{
		cThis.Controls.AddAt(index, new LiteralControl(literal));
	}
	/// <summary>
	/// Gets the <see cref="TA.Turbo.TPage"/> which contains this control.
	/// </summary>
	/// <param name="This"></param>
	/// <returns></returns>
	public static TPage get_TPage(this Control This)
	{
		return This.Page as TPage;
	}
	public static void BindDataEntry(this Control This, object dataEntry)
	{
		This.ForEachRecursive<BaseValueDisplayControl>(delegate(BaseValueDisplayControl c)
		{
			if (!string.IsNullOrEmpty(c.Data.Field) && (c.Data.Direction & DataDirection.Bind) == DataDirection.Bind)
			{
				object value = dataEntry.Eval(c.Data.Field);
				if (!string.IsNullOrEmpty(c.Data.Format))
				{
					value = string.Format(c.Data.Format, value);
				}
				else
				{
					if (c.Data.BindingType == ValidationDataType.String)
						value = (value ?? "").ToString();
				}
				c.GenericValue = value;
			}
		});
	}
}
